package ca.nfb.media.nfbPlayer.views
{
	import ca.nfb.media.nfbPlayer.ComModule;
	import ca.turbulent.media.Pyro;
	
	import flash.display.MovieClip;
	import flash.display.StageDisplayState;
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;
	import flash.geom.ColorTransform;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.system.System;
	import flash.text.AntiAliasType;
	import flash.text.Font;
	import flash.text.GridFitType;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	
	public class Sharing extends NFBPlayerView
	{
		public var linkTxt			:TextField;
		public var embedTxt			:TextField;
		
		private var format			:TextFormat;	
		private var selectedFormat	:TextFormat;	
		public var endPlaylistOn	:Boolean = false;
		
		public function Sharing()
		{
			super();
		}
		
		override public function close(evt:*=null):void
		{
			this.visible = false;
			core.overlayedPanelOpen = false;
			
			if (this.previousState == NFBPlayer.PREVIEW_IMAGE_STATE)
			{
				core.setCurrentState(NFBPlayer.PREVIEW_IMAGE_STATE);
				if (core.embeddedMode)
					core.embeddedWelcomeView.show();	
					
				if (core.warningMessageView.wasShowing)
					core.warningMessageView.visible = true;	
						
				return;	
			}
			
			if (!core.wasPaused)
			{
				if (this.previousState != NFBPlayer.END_PLAYLIST_STATE)
				{
					player.play();
				}
			}
			else
			{
				core.controlBar.overlay_mc.mouseEnabled = false;
				core.controlBar.overlay_mc.alpha = 0;	
			}
			
			if (this.endPlaylistOn)
			{
				endPlaylistOn = false;					
				core.finalPlaylistView.wasDrawnEarlier = true;		
				core.setCurrentState(NFBPlayer.END_PLAYLIST_STATE);
			}
			else
			{
				core.finalPlaylistView.wasDrawnEarlier = false;	
				core.setCurrentState(NFBPlayer.DEFAULT_STATE);
			}
		}
		
		
		override protected function checkLang():void
		{
			switch(ComModule.getLang())
			{
				case "EN":
				title_mc.gotoAndStop("en");
				networksTitle_mc.gotoAndStop("en");
				linkTitle_mc.gotoAndStop("en");
				embedTitle_mc.gotoAndStop("en");
				selectLinkButton_mc.label_mc.gotoAndStop(1);
				selectEmbedButton_mc.label_mc.gotoAndStop(1);
				break;
				
				case "FR":
				title_mc.gotoAndStop("fr");
				networksTitle_mc.gotoAndStop("fr");
				linkTitle_mc.gotoAndStop("fr");
				embedTitle_mc.gotoAndStop("fr");
				selectLinkButton_mc.label_mc.gotoAndStop(2);
				selectEmbedButton_mc.label_mc.gotoAndStop(2);
				break;
			}
		}
		
		override protected function addedToStage(evt:Event):void 
		{ 
			registerStageEvents();
			format = new TextFormat(core.mainFont.fontName, 14, 0xFFFFFF);
			selectedFormat = new TextFormat(core.mainFont.fontName, 14, 0xF69400); 
			x = 7;
			y = 7;
			this.visible = false;
			
			setProperties([closeButton_mc, sharingButton_mc, facebookButton_mc, redditButton_mc, stumbleUponButton_mc, diggButton_mc, deliciousButton_mc, googleButton_mc, selectLinkButton_mc, selectEmbedButton_mc], {buttonMode:true, tabEnabled:true, mouseEnabled:true, useHandCursor:true}, buttonHandler);
			
			core.screenReaderManager.addAccess(closeButton_mc, "Close", "Fermer");
			core.screenReaderManager.addAccess(facebookButton_mc, "Post on facebook", "Publier sur facebook");
			core.screenReaderManager.addAccess(redditButton_mc, "Post on reddit", "Publier sur reddit");
			core.screenReaderManager.addAccess(stumbleUponButton_mc, "Post on stumbleuppon", "Publier sur stumbleuppon");
			core.screenReaderManager.addAccess(diggButton_mc, "Post on digg", "Publier sur digg");
			core.screenReaderManager.addAccess(deliciousButton_mc, "Post on delicious", "Publier sur delicious");
			core.screenReaderManager.addAccess(deliciousButton_mc, "Post on google bookmarks", "Publier sur google bookmarks");
			core.screenReaderManager.addAccess(selectLinkButton_mc, "Select and copy link", "Sélectionner et copier le lien");
			core.screenReaderManager.addAccess(selectEmbedButton_mc, "Select and copy embed code", "Sélectionner et copier le code pour intégrer sur un site");
			
			
			
			// sharingButton_mc.addEventListener(MouseEvent.CLICK, close);
			// closeButton_mc.addEventListener(MouseEvent.CLICK, close);
			
			
			linkTxt = new TextField();
			linkTxt.defaultTextFormat = format;
			linkTxt.x = 15;
			linkTxt.y = 152;
			
			linkTxt.multiline = false;
			linkTxt.height = 18;
			linkTxt.width = 376;
			linkTxt.selectable = true;
			linkTxt.embedFonts = true;
			
			addChild(linkTxt);
			
			embedTxt = new TextField();
			embedTxt.defaultTextFormat = format;
			embedTxt.x = 15;
			embedTxt.y = 228;
			embedTxt.multiline = false;
			embedTxt.height = 18;
			embedTxt.width = 376;
			embedTxt.selectable = true;
			
			embedTxt.embedFonts = true;
			addChild(embedTxt);
			linkTxt.alwaysShowSelection = false;
			embedTxt.alwaysShowSelection = false;
			linkTxt.type = TextFieldType.DYNAMIC;
			embedTxt.type = TextFieldType.DYNAMIC;
			
			linkTxt.addEventListener(MouseEvent.CLICK, linkFocused, false, 0, true);
			embedTxt.addEventListener(MouseEvent.CLICK, embedFocused, false, 0, true);
			
			// linkTxt.addEventListener(FocusEvent.FOCUS_OUT, linkFocusedOut);
			// embedTxt.addEventListener(FocusEvent.FOCUS_OUT, embedFocusedOut);
		}

		private function linkFocused(evt:MouseEvent):void
		{
			embedTxt.background = false;
			embedTxt.setTextFormat(this.format, 0, embedTxt.length);
			
			linkTxt.background = true;
			linkTxt.backgroundColor = 0x000000;
			linkTxt.setSelection(0, linkTxt.length);
			var clipboard:String = linkTxt.text;
			System.setClipboard(clipboard);
			// linkTxt.setTextFormat(this.selectedFormat, 0, linkTxt.length);
			
		}
		
		private function linkFocusedOut(evt:FocusEvent):void
		{
			linkTxt.background = false;
			linkTxt.setTextFormat(this.format, 0, linkTxt.length);
		}
		
		private function embedFocused(evt:MouseEvent):void
		{
			embedTxt.setSelection(0, embedTxt.length);
			embedTxt.background = true;
			embedTxt.backgroundColor = 0x000000;
			// embedTxt.setTextFormat(this.selectedFormat, 0, embedTxt.length);
			var clipboard:String = embedTxt.text;
			System.setClipboard(clipboard);
			linkTxt.background = false;
			linkTxt.setTextFormat(this.format, 0, linkTxt.length);
			
		}
		
		private function embedFocusedOut(evt:FocusEvent):void
		{
			embedTxt.background = false;
			embedTxt.setTextFormat(this.format, 0, embedTxt.length);
		}
	
		private function buttonHandler(evt:MouseEvent):void
		{
			switch (evt.type.toLowerCase())
			{
				case "mouseover":
				switch (MovieClip(evt.currentTarget).name)
				{
					case "facebookButton_mc":
					case "redditButton_mc":
					case "stumbleUponButton_mc":
					case "diggButton_mc":
					case "deliciousButton_mc":
					case "googleButton_mc":
					MovieClip(evt.currentTarget).icon_mc.gotoAndStop(5);
					break;
					
					case "selectLinkButton_mc":
					case "selectEmbedButton_mc":
					MovieClip(evt.currentTarget).gotoAndStop(2);
					break;
				}
				break;
				
				case "mouseout":
				switch (MovieClip(evt.currentTarget).name)
				{
					case "facebookButton_mc":
					case "redditButton_mc":
					case "stumbleUponButton_mc":
					case "diggButton_mc":
					case "deliciousButton_mc":
					case "googleButton_mc":
					MovieClip(evt.currentTarget).icon_mc.gotoAndStop(1);
					break;
					
					case "selectLinkButton_mc":
					case "selectEmbedButton_mc":
					MovieClip(evt.currentTarget).gotoAndStop(1);
					break;
				}
				break;
				
				case "click":
				var url:String = "";
				var windowName:String = "";
				
				switch(MovieClip(evt.currentTarget).name)
				{
					case "facebookButton_mc":
					url = ComModule.getInstance().latestSharingSites['Facebook']['url'];
					windowName = "NFB-ONF --- Facebook";
					navigateToURL(new URLRequest(url), windowName);
					
					if (stage.displayState == StageDisplayState.FULL_SCREEN)
						player.toggleFullScreen();
					
					break;
					
					case "redditButton_mc":
					url = ComModule.getInstance().latestSharingSites['Reddit']['url'];
					windowName = "NFB-ONF --- Reddit";
					navigateToURL(new URLRequest(url), windowName);
					
					if (stage.displayState == StageDisplayState.FULL_SCREEN)
						player.toggleFullScreen();
					
					break;
					
					case "stumbleUponButton_mc":
					url = ComModule.getInstance().latestSharingSites['StumbleUpon']['url'];
					windowName = "NFB-ONF --- StumbleUpon";
					navigateToURL(new URLRequest(url), windowName);
				
					if (stage.displayState == StageDisplayState.FULL_SCREEN)
						player.toggleFullScreen();
					
					break;
					
					case "diggButton_mc":
					url = ComModule.getInstance().latestSharingSites['Digg']['url'];
					windowName = "NFB-ONF --- Digg";
					navigateToURL(new URLRequest(url), windowName);
				
					if (stage.displayState == StageDisplayState.FULL_SCREEN)
						player.toggleFullScreen();
					
					break;
					
					case "deliciousButton_mc":
					url = ComModule.getInstance().latestSharingSites['del.icio.us']['url'];
					windowName = "NFB-ONF --- del.icio.us";
					navigateToURL(new URLRequest(url), windowName);
					if (stage.displayState == StageDisplayState.FULL_SCREEN)
						player.toggleFullScreen();
					break;
					
					case "googleButton_mc":
					url = ComModule.getInstance().latestSharingSites['Google Bookmarks']['url'];
					windowName = "NFB-ONF --- Google Bookmarks";
					navigateToURL(new URLRequest(url), windowName);
					
					if (stage.displayState == StageDisplayState.FULL_SCREEN)
						player.toggleFullScreen();
					
					break;
					
					case "closeButton_mc":
					case "sharingButton_mc":
					close();
					break;
					
					case "selectLinkButton_mc":
					stage.focus = linkTxt;
					linkTxt.background = true;
					linkTxt.backgroundColor = 0x000000;
					var l:Number = linkTxt.length;
					linkTxt.setSelection(0, l);
					embedTxt.background = false;
					var clipboard:String = linkTxt.text;
					System.setClipboard(clipboard);
					break;
					
					case "selectEmbedButton_mc":
					stage.focus = embedTxt;
					embedTxt.background = true;
					embedTxt.backgroundColor = 0x000000;
					var l2:Number = embedTxt.length;
					embedTxt.setSelection(0, l2);
					linkTxt.background = false;				
					var clipboard2:String = embedTxt.text;
					System.setClipboard(clipboard2);
					break;
				}

			}
			
		}
		
		public function checkNetworks():void
		{
			if (ComModule.getInstance().latestSharingSites['del.icio.us'] != null)
			{
				MovieClip(deliciousButton_mc).alpha = 1;
				MovieClip(deliciousButton_mc).mouseEnabled = MovieClip(deliciousButton_mc).mouseChildren = true;
			}
			else
			{
				MovieClip(deliciousButton_mc).alpha = .35;
				MovieClip(deliciousButton_mc).mouseEnabled = MovieClip(deliciousButton_mc).mouseChildren = false;
			}
			
			if (ComModule.getInstance().latestSharingSites['Digg'] != null)
			{
				MovieClip(diggButton_mc).alpha = 1;
				MovieClip(diggButton_mc).mouseEnabled = MovieClip(diggButton_mc).mouseChildren = true;
			}
			else
			{
				MovieClip(diggButton_mc).alpha = .35;
				MovieClip(diggButton_mc).mouseEnabled = MovieClip(diggButton_mc).mouseChildren =false;
			}
			
			if (ComModule.getInstance().latestSharingSites['StumbleUpon'] != null)
			{
				MovieClip(stumbleUponButton_mc).alpha = 1;
				MovieClip(stumbleUponButton_mc).mouseEnabled = MovieClip(stumbleUponButton_mc).mouseChildren = true;
			}
			else
			{
				MovieClip(stumbleUponButton_mc).alpha = .35;
				MovieClip(stumbleUponButton_mc).mouseEnabled = MovieClip(stumbleUponButton_mc).mouseChildren = false;
			}
			
			if (ComModule.getInstance().latestSharingSites['Reddit'] != null)
			{
				MovieClip(redditButton_mc).alpha = 1;
				MovieClip(redditButton_mc).mouseEnabled = MovieClip(redditButton_mc).mouseChildren = true;
			}
			else
			{
				MovieClip(redditButton_mc).alpha = .35;
				MovieClip(redditButton_mc).mouseEnabled = MovieClip(redditButton_mc).mouseChildren = false;
			}
			
			if (ComModule.getInstance().latestSharingSites['Facebook'] != null)
			{
				MovieClip(facebookButton_mc).alpha = 1;
				MovieClip(facebookButton_mc).mouseEnabled = MovieClip(facebookButton_mc).mouseChildren = true;
			}	
			else
			{
				MovieClip(facebookButton_mc).alpha = .35;
				MovieClip(facebookButton_mc).mouseEnabled = MovieClip(facebookButton_mc).mouseChildren = false;
			}
			
			if (ComModule.getInstance().latestSharingSites['Google Bookmarks'] != null)
			{
				MovieClip(googleButton_mc).alpha = 1;
				MovieClip(googleButton_mc).mouseEnabled = MovieClip(googleButton_mc).mouseChildren = true;
			}
			else
			{
				MovieClip(googleButton_mc).alpha = .35;
				MovieClip(googleButton_mc).mouseEnabled = MovieClip(googleButton_mc).mouseChildren = false;
			}
		}
		
	
		override protected function coreStateUpdated(evt:Event=null):void 
		{ 
			if (core.currentState == NFBPlayer.SHARING_STATE)
			{
				core.overlayedPanelOpen = true;
				this.visible = true;
				
				if (ComModule.getInstance().latestEmbed != "")
				{
					this.embedTxt.text = ComModule.getInstance().latestEmbed;	
				}
				else
				{
					this.embedTxt.text = ComModule.getInstance().defaultEmbedURLs.prefix+core._movieID+ComModule.getInstance().defaultEmbedURLs.suffix;
				}
						
				this.linkTxt.text = ComModule.getInstance().latestDirectURL;
				
				embedTxt.setTextFormat(format, 0, embedTxt.length);
				linkTxt.setTextFormat(format, 0, linkTxt.length);
				embedTxt.background = false;
				linkTxt.background = false;
				
				this.checkNetworks();
			}
			else
			{	
				this.visible = false;	
			}
		}
		
		public static function setFieldSelectionColor(field:TextField, color:uint):void
		{
			field.backgroundColor = invert(field.backgroundColor);
			field.borderColor = invert( field.borderColor );
			field.textColor = invert( field.textColor );
				
			var colorTrans:ColorTransform = new ColorTransform();
			colorTrans.color = color;
			colorTrans.redMultiplier = -1;
			colorTrans.greenMultiplier = -1;
			colorTrans.blueMultiplier = -1;
			field.transform.colorTransform = colorTrans;
		}
		
		public static function invert(color:uint):uint
		{
			var colorTrans:ColorTransform = new ColorTransform();
			colorTrans.color = color;
			
			return invertColorTransform(colorTrans).color;
		}
		
		public static function invertColorTransform( colorTrans:ColorTransform ):ColorTransform
		{
			with( colorTrans )
			{
				redMultiplier = -redMultiplier;
				greenMultiplier = -greenMultiplier;
				blueMultiplier = -blueMultiplier;
				redOffset = 255 - redOffset;
				greenOffset = 255 - greenOffset;
				blueOffset = 255 - blueOffset;
			}
			
			return colorTrans;
		}

	}
}	